/**
 * 吐司效果
 * @param info
 */
function toast(info) {
    if($('#toast')[0] == undefined) {
        var toast = '<div id="toast" class="toast"></div>';
        $("body").append(toast);
    }
    $('#toast').html(info).fadeIn(300).delay(1000).fadeOut(300);
}

/**
 * 加载动画
 * @param info
 */
function loading(info) {
    if($('#loading')[0] == undefined) {
        var loading = '<div id="loading" class="model">\n' +
            '    <div class="loading">\n' +
            '        <div class="circle-loader">\n' +
            '            <div class="circle-line">\n' +
            '                <div class="circle circle-blue"></div>\n' +
            '                <div class="circle circle-blue"></div>\n' +
            '                <div class="circle circle-blue"></div>\n' +
            '            </div>\n' +
            '            <div class="circle-line">\n' +
            '                <div class="circle circle-yellow"></div>\n' +
            '                <div class="circle circle-yellow"></div>\n' +
            '                <div class="circle circle-yellow"></div>\n' +
            '            </div>\n' +
            '            <div class="circle-line">\n' +
            '                <div class="circle circle-red"></div>\n' +
            '                <div class="circle circle-red"></div>\n' +
            '                <div class="circle circle-red"></div>\n' +
            '            </div>\n' +
            '            <div class="circle-line">\n' +
            '                <div class="circle circle-green"></div>\n' +
            '                <div class="circle circle-green"></div>\n' +
            '                <div class="circle circle-green"></div>\n' +
            '            </div>\n' +
            '        </div>\n' +
            '        <div id="loadingTip" class="loading-tip">请稍等...</div>\n' +
            '    </div>\n' +
            '</div>';
        $("body").append(loading);
        $('#loadingTip').html(info);
        $('#loading').show();
    }
}

/**
 * 关闭加载动画
 */
function closeLoading() {
    $('#loading').hide();
}


/**
 * 吐司效果
 * @param info
 */
function winTip(tipContent, flag) {
    var src = '';
    if(flag) {
        src = '/resources/icon/yes.png';
    } else {
        src = '/resources/icon/fail.png';
    }
    if($('#winTip')[0] == undefined) {
        var toast = '<div id="winTip" class="model">\n' +
            '\t<div class="win-tip" style="z-index: 999;">\n' +
            '\t\t<div class="tip">\n' +
            '\t\t\t<img id="tipImg" src="'+ src +'" alt=""/>\n' +
            '\t\t\t<p id="tipContent"></p>\n' +
            '\t\t\t<span id="winBtn" class="gq-btn">我知道了</span>\n' +
            '\t\t\t</div>\n' +
            '\t\t<br class="clear"/>\n' +
            '\t</div>\n' +
            '\t<br class="clear"/>\n' +
            '</div>';
        $("body").append(toast);
    }
    $('#tipContent').html(tipContent);
    $('#tipImg').attr('src', src);
    $('#winTip').show();
    $('#winBtn').click(function () {
        $('#winTip').hide();
    });
}

$(function () {

    /*图片的缩放*/
    $('.video-img').mouseenter(function(){
        var wValue=1.5 * $(this).width();
        var hValue=1.5 * $(this).height();
        $(this).stop().animate({width: wValue,
            height: hValue,
            left:("-"+(0.5 * $(this).width())/2),
            top:("-"+(0.5 * $(this).height())/2)}, 300);
    }).mouseleave(function(){
        $(this).stop().animate({width: "300",
            height: "240",
            left:"0px",
            top:"0px"}, 300 );
    });

});


function isEmpty(str) {
    return str.length == 0;
}

/**
 * 加入购物车
 * @param cookbookid
 */
function shoppingCart(cookbookid) {
    $.post('/shopCart', {"cookbookid":cookbookid}, function (result) {
        if(result.code == -1) {
            toast('加入购物车，请先登录！');
            return ;
            //收藏
        } else if(result.code == 1) {
            toast('加入购物车成功！');
        } else {
            toast('从购物车中移除！');
        }
    });
}

(function ($) {
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
})(jQuery);

$(function () {

    /*显示隐藏食谱信息*/
    $('.info1').click(function () {
        var ci = $(this).next().next().next();
        var ci2 = $(this).next().next().next().next();
        //显示隐藏
        $(ci).show();
        $(ci2).hide();
        //切换背景
        var info1 = $(this);
        var info2 = $(this).next();
        $(info1).addClass('info-bg');
        $(info2).removeClass('info-bg');
        return false;
    });
    $('.info2').click(function () {
        var ci = $(this).next().next();
        var ci2 = $(this).next().next().next();
        //显示隐藏
        $(ci).hide();
        $(ci2).show();
        //切换背景
        var info2 = $(this);
        var info1 = $(this).prev();
        $(info2).addClass('info-bg');
        $(info1).removeClass('info-bg');
        return false;
    });

    //收藏
    $('.collect').click(function () {
        var cookbookid = $(this).attr('data');
        $.post('/collectCookbook', {"cookbookid":cookbookid}, function (result) {
            if(result.code == -1) {
                toast('收藏，请先登录！');
                return ;
                //收藏
            } else if(result.code == 1) {
                toast('收藏成功！');
            } else {
                toast('取消收藏！');
            }
        });
        return false;
    });
    //购买
    $('.shopping-cart').click(function () {
        var cookbookid = $(this).attr('data');
        shoppingCart(cookbookid);
        return false;
    });
    
});